var news;
$(function () {
    news = new Vue({
        el:'#newsController',
        data:{
            list:[]
        },
        methods:{
            loadData:function () {
                var that = this;
                sy.post('/web/index/indexNews',{
                    page:1,
                    limit:4
                },function (data) {
                    for (var i = 0; i < data.length; i++){
                        var item = data[i];
                        item.time = item.createTime.substring(0,16);
                        item.date = item.createTime.substring(8,10);
                    }
                    that.list = data;
                })
            },
            jump:function (data) {
                // if (data.type == 2){
                //     window.open(data.targetContent);
                // }else{
                //     if (data.redType  == 0){
                //         location.href = sy.path+"/web/artist/"+data.targetContent;
                //     }else{
                //         location.href = sy.path+"/web/art/"+data.targetContent;
                //     }
                //
                // }
                if (data.type == 2){
                    window.open(data.targetContent);
                }else{
                    location.href = sy.path+'/web/index/news/'+data.id;
                }

            }
        },
        created:function () {
            this.loadData();
        }
    });
    var art = new Vue({
        el:'#artController',
        data:{
            art:{}
        },
        methods:{
            loadArt:function () {
                var that = this;
                sy.post("/api/index/indexTodayGoods",{},function (data) {
                    if (data.errorCode == 200){
                        that.art = data.data;
                    }
                })
            }
        },
        mounted:function () {
            this.loadArt();
        }
    });
    //排行榜
    var sort = new Vue({
        el: '#sortController',
        data:{
            sort:[]
        },
        methods:{
            loadSort:function () {
                var that = this;
                sy.postApi("/api/index/getHotList",{limit:16},function (data) {
                    var item = [];
                    for (var i = 0; i < data.length; i++){
                        item.push(data[i]);
                        if (item.length == 8){
                            that.sort.push(item);
                            item = [];
                        }else if (i == data.length - 1){
                            that.sort.push(item);
                        }
                    }
                })
            }

        },
        mounted:function () {
            var that = this;
            that.loadSort();
        },
        updated:function () {
            sy.loadScript('/static/web/js/carousel.js')
        }
    });


    //焦点图
    var focus = new Vue({
        el: '#focusController',
        data:{
            focus:[]
        },
        methods:{
            loadFocus:function () {
                var that = this;
                sy.postApi("/api/index/indexBannerList",{},function (data) {
                    that.focus = data;
                })
            },
            getBackStyle:function (img) {
                return 'url('+sy.path+'/download?id='+img+') 50% 0 no-repeat';
            },
            jump:function (item) {
                if (item.type == 1 && item.targetContent){
                    if (item.redType == 0){
                        //艺术家
                        location.href = sy.path+"/web/artist/"+item.targetContent;
                    }else{
                        location.href = sy.path+"/web/art/"+item.targetContent;
                    }

                }else if(item.type == 2){
                    window.open(item.targetContent);
                }
            }
        },
        created:function () {
            var that = this;
            that.loadFocus();
        },
        mounted:function () {

        },
        updated:function () {
            $('.flexslider').flexslider({
                directionNav: true,
                pauseOnAction: false
            });
        }
    });
    var container;
    //热拍
    var hot = new Vue({
        el: '#hotController',
        data:{
            hot:[],
            nowNum:'',
            now:new Date().format("yyyy-MM-dd")
        },
        methods:{
            loadHot:function () {
                var that = this;
                sy.postApi("/api/index/indexHotPawnList",{
                    page:1,
                    limit:8
                },function (data) {
                    that.hot = data;
                    that.calSecond();
                    Vue.nextTick(function () {
                        var $newElems = $('.item').appendTo(container);
                        $newElems.imagesLoaded(function(){
                            $newElems.animate({ opacity: 1},800);
                            container.masonry( 'appended', $newElems,true);
                        });
                        // container.masonry( 'reload');
                    });
                })
            },
            calSecond:function () {
                var that = this;
                $.each(that.hot,function (i,o) {
                    if (o.pawnCountDown){
                        that.$set(o, 'time', DateUtil.formatSecondsToHMS(o.pawnCountDown--));
                    }
                })
                setTimeout(function(){
                    that.calSecond();
                },1000)
            },
            jump:function (item) {
                location.href = sy.path+'/web/auction/'+item.goodsId;
            },
            getTimeNum:function () {
                var that = this;
                sy.postApi('/api/index/getPawnTime',{times:that.now},function (data) {
                    that.nowNum = data;
                })
            },
            init:function () {
                tores();
                container = $('.waterfull ul');
                container.imagesLoaded(function(){
                    container.masonry({
                        columnWidth: 320,
                        itemSelector : '.item',
                        isFitWidth: true,//是否根据浏览器窗口大小自动适应默认false
                        isAnimated: true,//是否采用jquery动画进行重拍版
                        isRTL:false,//设置布局的排列方式，即：定位砖块时，是从左向右排列还是从右向左排列。默认值为false，即从左向右
                        isResizable: true,//是否自动布局默认true
                        animationOptions: {
                            duration: 800,
                            easing: 'easeInOutBack',//如果你引用了jQeasing这里就可以添加对应的动态动画效果，如果没引用删除这行，默认是匀速变化
                            queue: false//是否队列，从一点填充瀑布流
                        }
                    });
                });
            }
        },
        created:function () {
            var that = this;
            that.loadHot();
            that.getTimeNum();
        },
        mounted:function () {
            this.init();
        },
        updated:function () {
        }
    });

});